package com.ccl.iot.batch;

import android.content.SharedPreferences;
import android.text.TextUtils;

import com.ccl.iot.IOTNameMonitor;
import com.ccl.iot.IOTObject;
import com.ccl.iot.device.BatchIntentionDevice;
import com.ccl.iot.device.IOTDevice;
import com.ccl.iot.huiju.IOTHuiJu;
import com.ccl.iot.managers.DeviceManager;
import com.ccl.iot.managers.Manager;
import com.ccl.iot.object.IOTBatchItemInfo;
import com.ccl.iot.publics.StringUtils;
import com.ccl.iot.publics.ThreadPoolUtils;

import java.io.Serializable;
import java.util.List;

import static com.ccl.iot.dvr.utils.Consts.BATCH_SPLIT;
import static com.ccl.iot.dvr.utils.Consts.BATCH_SPLIT_TEMP;

public class IOTBatch extends IOTNameMonitor implements IOTObject.IOTNamedObject, IOTHuiJu,Serializable {
    private long iID;
    private String mNameAndImgIndex;

    private boolean isOffLine;

    private List<IOTBatchItemInfo> mBatchItemInfo;

    protected IOTBatchInterface iBatchInterface = null;
    private final DeviceManager mDeviceManager;

    public IOTBatch(long batchID, String nameAndImgIndex) {
        iID = batchID;
        mNameAndImgIndex = nameAndImgIndex;

        //===============================modify start===============================
        mIsAdd2Intention = mSp.getBoolean(iID + "", false);
        //===============================modify end===============================
        mDeviceManager = DeviceManager.GetDefaultManager();
    }

    @Override
    public boolean equals(Object o) {
        if (super.equals(o) == false) {
            if (o != null && o instanceof IOTBatch) {
                return ((IOTBatch) o).getID() == getID();
            }
            return false;
        } else {
            return true;
        }
    }

    @Override
    public long getID() {
        return iID;
    }

    @Override
    public String getName() {
//        String name = StringUtils.substringBeforeLast(mNameAndImgIndex, BATCH_SPLIT);
//        if (TextUtils.isEmpty(name)) {
//            return "";
//        }
//        return name;

//        LogUtil.e("mNameAndImgIndex:"+ mNameAndImgIndex);
        int indexOf = mNameAndImgIndex.indexOf(BATCH_SPLIT);
        int indexOfTemp = mNameAndImgIndex.indexOf(BATCH_SPLIT_TEMP);
        String name = mNameAndImgIndex;

        if (indexOf != -1 && indexOfTemp != -1){
            name = StringUtils.substringBeforeLast(mNameAndImgIndex, BATCH_SPLIT_TEMP);
        }else if (indexOfTemp != -1){
            name = StringUtils.substringBeforeLast(mNameAndImgIndex, BATCH_SPLIT_TEMP);
        }else if (indexOf != -1){
            name = StringUtils.substringBeforeLast(mNameAndImgIndex, BATCH_SPLIT);
        }

        if (TextUtils.isEmpty(name)) {
            return "";
        }
        return name;

    }

    public String getNameAndImgIndex() {
        return mNameAndImgIndex;
    }

    /**
     * 获取图片的下标
     *
     * @return
     */
    public int getImgIndex() {
//        String indexString = StringUtils.substringAfterLast(mNameAndImgIndex, BATCH_SPLIT);
//        int index = 0;
//        if(indexString != null){
//            try {
//                index = Integer.valueOf(indexString);
//                if (index < 0) {
//                    index = 0;
//                }
//            } catch (NumberFormatException e) {
//                e.printStackTrace();
//            }
//        }
//        return index;

        int rawImageIndex = getRawImageIndex() & 0xffffff;
        if(rawImageIndex < 0){
            rawImageIndex = 0;
        }
        return rawImageIndex;
    }

    public int getRawImageIndex(){
        String indexString = null;
        int indexOf = mNameAndImgIndex.indexOf(BATCH_SPLIT);
        int indexOfTemp = mNameAndImgIndex.indexOf(BATCH_SPLIT_TEMP);

        if (indexOf != -1 && indexOfTemp != -1){
            indexString = StringUtils.substringAfterLast(mNameAndImgIndex, BATCH_SPLIT);
        }else if (indexOfTemp != -1){
            indexString = StringUtils.substringAfterLast(mNameAndImgIndex, BATCH_SPLIT_TEMP);
        }else if (indexOf != -1){
            indexString = StringUtils.substringAfterLast(mNameAndImgIndex, BATCH_SPLIT);
        }

        int index = 0;
        if(indexString != null){
            try {
                index = Integer.valueOf(indexString);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
        return index;
    }

    public boolean setNameAndImgIndex(Object aSetter, String nameAndImgIndex) {
        if (nameAndImgIndex != mNameAndImgIndex) {
            if (aSetter == iBatchInterface || (iBatchInterface != null && iBatchInterface.setName(this, nameAndImgIndex))) {
                mNameAndImgIndex = nameAndImgIndex;
                super.onNameChange(nameAndImgIndex);
                return true;
            }
        }
        return false;
    }

    public boolean isContainBlIr() {
        int imgIndex = getRawImageIndex();
        return (imgIndex >> 30) > 0;
    }

    /**
     * 设置batch的名字和图片下标
     *
     * @param nameAndImgIndex
     * @return
     */
    @Override
    public boolean setName(String nameAndImgIndex) {
        return setNameAndImgIndex(this, nameAndImgIndex);
    }

    public void setBatchInterfce(IOTBatchInterface aInterface) {
        iBatchInterface = aInterface;
    }

    public boolean execute() {
        if (isOffLine){
            //执行2次
            executeOffBatch();
            executeOffBatch();
        }else {
            if (iBatchInterface != null) {
                return iBatchInterface.execute(this);
            }
        }

        return false;
    }

    public boolean executeOffBatch(){
        if (mBatchItemInfo != null && mBatchItemInfo.size() > 0 && mDeviceManager != null){
            ThreadPoolUtils.getInstance().addTask(new Runnable() {
                @Override
                public void run() {
                    int size = mBatchItemInfo.size();
                    for (int i = 0; i < size; i++) {
                        IOTBatchItemInfo itemInfo = mBatchItemInfo.get(i);
                        if (itemInfo != null){
                            IOTDevice device = mDeviceManager.findDevice(itemInfo.iDevID);
                            if (device != null){
                                device.writeRegister(itemInfo.iAddress,itemInfo.iSize,itemInfo.iData,2);
                                try {
                                    Thread.sleep(500);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }

                }
            });
            return true;
        }
        return false;
    }

    //===============================modify start===============================
    private boolean mIsAdd2Intention = false;
    public static final SharedPreferences mSp = Manager.DBCONTEXT.getSharedPreferences("IOTBATCH", 0);

    public boolean isAdd2Intention() {
        return mIsAdd2Intention;
    }

    public BatchIntentionDevice mBatchIntentionDevice;

    public BatchIntentionDevice add2Intention(boolean value) {
        mIsAdd2Intention = value;
        mSp.edit().putBoolean(iID + "", value).commit();
        return new BatchIntentionDevice(this);
    }
    //===============================modify end===============================

    public void remove2Intention(boolean value) {
        mIsAdd2Intention = value;
        mSp.edit().putBoolean(iID + "", value).commit();
    }


    private int mPositionIndex = -1;
    private int mPageIndex = -1;

    public int getPositionIndex() {
        return mPositionIndex;
    }

    public void setPositionIndex(int positionIndex) {
        mPositionIndex = positionIndex;
    }

    public int getPageIndex() {
        return mPageIndex;
    }

    public void setPageIndex(int pageIndex) {
        mPageIndex = pageIndex;
    }

    public List<IOTBatchItemInfo> getBatchItemInfo(){
        return mBatchItemInfo;
    }

    public void setBatchItmeInfo(List<IOTBatchItemInfo> batchItemInfo){
        mBatchItemInfo = batchItemInfo;
    }

    public void setOffLine(boolean isOffLineF){
        isOffLine = isOffLineF;
    }

    public boolean isOffLine(){
        return isOffLine;
    }
}
